package com.zxl.live.screen.screen.ui.widget.panel;

import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.zxl.live.screen.screen.utils.ScreenUtils;

import java.util.Random;

public class DandelionScreenSpirit extends ScreenSpirit {

	public DandelionScreenSpirit(Bitmap bitmap, Random mRandom) {
		super(bitmap, mRandom);
	}

	@Override
	public void doDraw(Camera camera, Matrix matrix, Canvas canvas, Paint paint) {
		if (isActive) {
			doDraw(canvas, paint, bitmap, scale, 255, baseX, baseY, x, y, true, angle);
		}
	}
	
	@Override
	public void logic() {
		if (isActive) {
			changeX();
			changeY();
			changeAngle();
			if (baseY + y - bitmap.getHeight() * 1.5 > ScreenUtils.sHeightPixels
					|| baseX + x - bitmap.getWidth() * 2 > ScreenUtils.sWidthPixels) {
				reset();
				return;
			}
		}
	}
	
	private void doDraw(Canvas canvas, Paint paint, Bitmap bitmap, float scale, int alpha, float baseX, float baseY, 
			float offsetX, float offsetY, boolean isNeedRotate, int angle) {
		if (bitmap == null || bitmap.isRecycled()) {
			return;
		}
		int saveCount = canvas.save();
		canvas.translate(baseX - bitmap.getWidth() / 2 + offsetX, baseY - bitmap.getHeight() / 2 + offsetY);
		canvas.scale(scale, scale);

		if (isNeedRotate) {
			canvas.rotate(angle, bitmap.getWidth() * 0.5f, bitmap.getHeight() * 0.5f);
		}

		if (alpha < 0) {
			paint.setAlpha(0);
		} else if (alpha > 255) {
			paint.setAlpha(255);
		} else {
			paint.setAlpha(alpha);
		}

		canvas.drawBitmap(bitmap, 0, 0, paint);
		paint.setAlpha(255);
		canvas.restoreToCount(saveCount);
	}
	
	public void changeAngle() {
		angle += rotateAngle;
		if (angle > 360) {
			angle = 0;
		}
	}
	
	public void changeX() {
		tmp++;
		if (tmp > 60) {
			tmp = 0;
			flyingAngle = mRandom.nextInt(360);
		}
		x += speed + (Math.sin((float) flyingAngle / 180 * Math.PI) * 0.25f) + 1f;
	}

	public void changeY() {
		tmp++;
		if (tmp > 60) {
			tmp = 0;
			flyingAngle = mRandom.nextInt(360);
		}
		y += (Math.sin((float) flyingAngle / 180 * Math.PI) * 0.25f) + 1f;
	}
	
	public void reset() {
		isActive = true;
		baseX = -mRandom.nextInt(ScreenUtils.sBaseWidthPixels);
		baseY = mRandom.nextInt(ScreenUtils.sBaseHeightPixels);
		
		y = -ScreenUtils.sBaseHeightPixels / 2 + mRandom.nextInt((int) (ScreenUtils.sBaseHeightPixels * 1.5f));
		
		if (y < -ScreenUtils.sBaseHeightPixels / 4) {
			x = mRandom.nextInt(ScreenUtils.sBaseWidthPixels / 4);
		} else {
			x = -mRandom.nextInt(ScreenUtils.sBaseWidthPixels / 2);
		}
		
		alpha = 125;
		isAlphaAdd = mRandom.nextBoolean();
		alphaSpeed = 1 + mRandom.nextInt(6);
		rotateAngle = mRandom.nextInt(10);
		rotateAngleAdd = 1 + mRandom.nextInt(40) * 0.1f;
		isRotateAngleAdd = mRandom.nextBoolean();
		flyingAngle = mRandom.nextInt(360);

		speed = mRandom.nextInt(20) * 0.1f;
		scale = (0.75f + 0.01f * mRandom.nextInt(50)) * 0.8f;

		rotateX = -80 + mRandom.nextInt(160);
		rotateY = -80 + mRandom.nextInt(160);
		rotateXAdd = 0.5f + mRandom.nextInt(30) * 0.1f;
		rotateYAdd = 0.5f + mRandom.nextInt(30) * 0.1f;
		isRotateXAdd = mRandom.nextBoolean();
		isRotateYadd = mRandom.nextBoolean();
		
		rotateAngle = mRandom.nextInt(8) + 2;
		speed = mRandom.nextInt(10) * 0.1f * rotateAngle;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}

}
